import configs
import time
from template.singleton import Singleton
from collections import deque


class Combat_mode(Singleton):
    # Перенести ветку экзоскелет или лабаратория или охранная система
    def __init__(self) -> None:
        self.temp_enabling_activation = None
        self.temp_permission_check_speech = None

    def health_status(self):
        pass

    def off_security_system(self):
        pass

    def health(self):
        pass

    def activation_combat_mode(self):
        self.temp_enabling_activation = configs.enabling_activation
        self.temp_permission_check_speech = configs.permission_check_speech

    def shutdown_combat_mode(self):
        configs.enabling_activation = self.temp_enabling_activation
        configs.permission_check_speech = self.temp_permission_check_speech


class Work_mode:
    def __init__(self) -> None:
        self.timer_name = configs.KERNEL_START_TIME
        self.dict_func = {}
        self.deque_message = deque()
        self.list_name_permission = []
        self.permission = ""

    def get_message(self, p):
        message = ""
        if len(self.deque_message) != 0:
            if p == 0:
                message = self.deque_message.popleft()
            else:
                message = self.deque_message.pop()

        return message

    def func_permitted(self, list_name):
        self.list_name_permission = list_name
        self.permission = "permitted"

    def func_ban(self, list_name):
        self.list_name_permission = list_name
        self.permission = "ban"

    def add_func(self, id_func, params, params_func=None):
        self.dict_func[id_func.__name__] = {
            "id": id_func,
            "params": params,
            "params_func": params_func,
        }

    def del_func(self, name):
        self.dict_func.pop(name)

    def get_func(self, name):
        return self.dict_func[name]

    def event_loop(self):
        list_key_func = self.dict_func.keys()
        for key in list_key_func:
            if not self.permission:
                self.__set_data(key)

            elif self.permission == "permitted":
                self.permission = ""
                self.list_name_permission = []
                if key in self.list_name_permission:
                    self.__set_data(key)

            elif self.permission == "ban":
                self.permission = ""
                self.list_name_permission = []
                if key in self.list_name_permission:
                    pass

    def __set_data(self, key):
        id_func = self.dict_func[key]["id"]
        params_func = self.dict_func[key]["params_func"]
        params = self.dict_func[key]["params"]
        old_time_update = params["old_time_update"]
        timing = params["timing"]
        # print(time.time() - (old_time_update + timing))
        if old_time_update + timing <= time.time():
            if params_func:
                mess = id_func(params_func)
            else:
                mess = id_func()
            self.dict_func[key]["params"]["old_time_update"] = time.time()
            self.deque_message.append(mess)
